{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "     id name   age\n",
      "aa  101   张三  10.0\n",
      "bb  102   李四  20.0\n",
      "cc  103   王五  30.0\n",
      "dd  104   赵六  40.0\n",
      "ee  105   冯七   NaN\n",
      "     id name   age\n",
      "cc  103   王五  30.0\n",
      "dd  104   赵六  40.0\n",
      "ee  105   冯七   NaN\n",
      "ff  106   周八  60.0\n",
      "aa  101   张三  10.0\n",
      "       id   name    age\n",
      "aa  False  False  False\n",
      "bb  False  False  False\n",
      "cc   True  False  False\n",
      "dd  False  False  False\n",
      "ee  False  False  False\n",
      "ff   True  False  False\n",
      "aa  False  False  False\n",
      "       id   name    age\n",
      "aa  False  False  False\n",
      "bb  False  False  False\n",
      "cc  False  False  False\n",
      "dd  False  False  False\n",
      "ee  False  False   True\n",
      "ff  False  False  False\n",
      "aa  False  False  False\n",
      "--------------------------------------------------\n",
      "170.0\n",
      "28.333333333333332\n",
      "10.0\n",
      "60.0\n",
      "376.66666666666663\n",
      "19.407902170679517\n",
      "25.0\n",
      "0    10.0\n",
      "Name: age, dtype: float64\n",
      "--------------------------------------------------\n",
      "25.0\n",
      "               id        age\n",
      "count    7.000000   6.000000\n",
      "mean   103.142857  28.333333\n",
      "std      1.951800  19.407902\n",
      "min    101.000000  10.000000\n",
      "25%    101.500000  12.500000\n",
      "50%    103.000000  25.000000\n",
      "75%    104.500000  37.500000\n",
      "max    106.000000  60.000000\n",
      "---www----------www----------www----------www----------www-------\n",
      "<class 'pandas.core.frame.DataFrame'>\n",
      "Index: 7 entries, aa to aa\n",
      "Data columns (total 3 columns):\n",
      " #   Column  Non-Null Count  Dtype  \n",
      "---  ------  --------------  -----  \n",
      " 0   id      7 non-null      int64  \n",
      " 1   name    7 non-null      object \n",
      " 2   age     6 non-null      float64\n",
      "dtypes: float64(1), int64(1), object(1)\n",
      "memory usage: 224.0+ bytes\n",
      "None\n",
      "---www----------www----------www----------www----------www-------\n",
      "id   name  age \n",
      "101  张三    10.0    2\n",
      "102  李四    20.0    1\n",
      "103  王五    30.0    1\n",
      "104  赵六    40.0    1\n",
      "106  周八    60.0    1\n",
      "dtype: int64\n",
      "---sss----------sss----------sss----------sss----------sss-------\n",
      "id      7\n",
      "name    7\n",
      "age     6\n",
      "dtype: int64\n"
     ]
    }
   ],
   "source": [
    "df = pd.DataFrame(data={\"id\": [101, 102, 103,104,105,106,101], \"name\": [\"张三\", \"李四\", \"王五\",\"赵六\",\"冯七\",\"周八\",\"张三\"], \"age\": [10, 20, 30, 40, None, 60,10]},index=[\"aa\", \"bb\", \"cc\", \"dd\", \"ee\", \"ff\",\"aa\"])\n",
    "# head()    查看前n行数据，默认5行\n",
    "print(df.head())\n",
    "# tail()    查看后n行数据，默认5行\n",
    "print(df.tail())\n",
    "# isin()    元素是否包含在参数集合中\n",
    "print(df.isin([103,106]))\n",
    "# isna()    元素是否为缺失值\n",
    "print(df.isna())\n",
    "print(\"----------\"*5)\n",
    "# sum() 求和\n",
    "print(df[\"age\"].sum())\n",
    "# mean()    平均值\n",
    "print(df[\"age\"].mean())\n",
    "# min() 最小值\n",
    "print(df[\"age\"].min())\n",
    "# max() 最大值\n",
    "print(df[\"age\"].max())\n",
    "# var() 方差\n",
    "print(df[\"age\"].var())\n",
    "# std() 标准差\n",
    "print(df[\"age\"].std())\n",
    "# median()  中位数\n",
    "print(df[\"age\"].median())\n",
    "# mode()    众数\n",
    "print(df[\"age\"].mode())\n",
    "print(\"----------\"*5)\n",
    "# quantile()    指定位置的分位数，如quantile(0.5)\n",
    "print(df[\"age\"].quantile(0.5))\n",
    "# describe()    常见统计信息\n",
    "print(df.describe())\n",
    "# info()    基本信息\n",
    "print(\"---www-------\"*5)\n",
    "print(df.info())\n",
    "print(\"---www-------\"*5)\n",
    "# value_counts()    每个元素的个数\n",
    "print(df.value_counts())\n",
    "print(\"---sss-------\"*5)\n",
    "# count()   非空元素的个数\n",
    "print(df.count())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "aa    False\n",
      "bb    False\n",
      "cc    False\n",
      "dd    False\n",
      "ee    False\n",
      "ff    False\n",
      "aa     True\n",
      "dtype: bool\n",
      "---www----------www----------www----------www----------www-------\n",
      "     id name  age\n",
      "ee  105   冯七  NaN\n",
      "----------------\n",
      "     id name   age\n",
      "aa  101   张三  10.0\n",
      "bb  102   李四  haha\n",
      "cc  103   王五  30.0\n",
      "dd  104   赵六  40.0\n",
      "ee  105   冯七   NaN\n",
      "ff  106   周八  60.0\n",
      "aa  101   张三  10.0\n",
      "True\n",
      "--------------------------------------------------------------------------------\n",
      "   A  B\n",
      "0  2  1\n",
      "1  5  6\n",
      "2  5  6\n",
      "3  7  8\n",
      "4  7  8\n",
      "--------index----------------index----------------index----------------index----------------index--------\n",
      "   A  B\n",
      "0  2  2\n",
      "1  5  6\n",
      "2  3  3\n",
      "3  7  8\n",
      "4  4  4\n",
      "--------------------------------------------------------------------------------\n",
      "   A  B\n",
      "0  2  1\n",
      "1  2  1\n",
      "2  2  1\n",
      "3  2  1\n",
      "4  2  1\n",
      "    A   B\n",
      "0   2   1\n",
      "1   7   7\n",
      "2  10   9\n",
      "3  17  17\n",
      "4  21  20\n",
      "     A    B\n",
      "0    2    1\n",
      "1   10    6\n",
      "2   30   12\n",
      "3  210   96\n",
      "4  840  288\n",
      "     A    B\n",
      "0  NaN  NaN\n",
      "1  3.0  5.0\n",
      "2 -2.0 -4.0\n",
      "3  4.0  6.0\n",
      "4 -3.0 -5.0\n",
      "     id name   age\n",
      "aa  101   张三  10.0\n",
      "aa  101   张三  10.0\n",
      "bb  102   李四  20.0\n",
      "cc  103   王五  30.0\n",
      "dd  104   赵六  40.0\n",
      "ee  105   冯七   NaN\n",
      "ff  106   周八  60.0\n",
      "     id name   age\n",
      "aa  101   张三  10.0\n",
      "aa  101   张三  10.0\n",
      "bb  102   李四  20.0\n",
      "cc  103   王五  30.0\n",
      "dd  104   赵六  40.0\n",
      "ff  106   周八  60.0\n",
      "ee  105   冯七   NaN\n",
      "     id name   age\n",
      "ff  106   周八  60.0\n",
      "dd  104   赵六  40.0\n",
      "     id name   age\n",
      "aa  101   张三  10.0\n"
     ]
    }
   ],
   "source": [
    "# drop_duplicates() 去重  duplicated()判断是否为重复行\n",
    "print(df.duplicated(subset=\"age\"))\n",
    "print(\"---www-------\"*5)\n",
    "# sample()  随机采样\n",
    "print(df.sample())\n",
    "# replace() 用指定值代替原有值\n",
    "print(\"----------------\")\n",
    "print(df.replace(20,\"haha\"))\n",
    "# equals()  判断两个DataFrame是否相同\n",
    "df1 = pd.DataFrame(data={\"id\": [101, 102, 103], \"name\": [\"张三\", \"李四\", \"王五\"], \"age\": [10, 20, 30]})\n",
    "df2 = pd.DataFrame(data={\"id\": [101, 102, 103], \"name\": [\"张三\", \"李四\", \"王五\"], \"age\": [10, 20, 30]})\n",
    "print(df1.equals(df2))\n",
    "# cummax()  累计最大值\n",
    "df3 = pd.DataFrame({'A': [2, 5, 3, 7, 4],'B': [1, 6, 2, 8, 3]})\n",
    "print(\"----------------\"*5)\n",
    "# 按列  等价于axis=0 默认\n",
    "print(df3.cummax(axis=\"index\"))\n",
    "print(\"--------index--------\"*5)\n",
    "# 按行  等价于axis=1\n",
    "bprint(\"----------------\"*5)\n",
    "# cummin()  累计最小值\n",
    "print(df3.cummin())\n",
    "# cumsum()  累计和\n",
    "print(df3.cumsum())\n",
    "# cumprod() 累计积\n",
    "print(df3.cumprod())\n",
    "# diff()    一阶差分\n",
    "print(df3.diff())\n",
    "# sort_index()  按行索引排序\n",
    "print(df.sort_index())\n",
    "# sort_values() 按某列的值排序，可传入列表来按多列排序，并通过ascending参数设置升序或降序\n",
    "print(df.sort_values(by=\"age\"))\n",
    "# nlargest()    返回某列最大的n条数据\n",
    "print(df.nlargest(n=2,columns=\"age\"))\n",
    "# nsmallest()   返回某列最小的n条数据\n",
    "print(df.nsmallest(n=1,columns=\"age\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "101    False\n",
      "104     True\n",
      "103     True\n",
      "102    False\n",
      "Name: age, dtype: bool\n",
      "********************\n",
      "    name  age\n",
      "104   李四   30\n",
      "103   王五   40\n"
     ]
    }
   ],
   "source": [
    "df = pd.DataFrame(data={\"age\": [20, 30, 40, 10], \"name\": [\"张三\", \"李四\", \"王五\", \"赵六\"]},columns=[\"name\", \"age\"],index=[101, 104, 103, 102],)\n",
    "print(df[\"age\"] > 25)\n",
    "print('*'*20)\n",
    "\n",
    "#数据过滤\n",
    "print(df[df[\"age\"] > 25])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "     name  age\n",
      "101  张三张三   40\n",
      "104  李四李四   60\n",
      "103  王五王五   80\n",
      "102  赵六赵六   20\n"
     ]
    }
   ],
   "source": [
    "df = pd.DataFrame(data={\"age\": [20, 30, 40, 10], \"name\": [\"张三\", \"李四\", \"王五\", \"赵六\"]},columns=[\"name\", \"age\"],index=[101, 104, 103, 102],)\n",
    "print(df * 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "     name   age\n",
      "101   NaN   NaN\n",
      "102  李四张三  30.0\n",
      "103  王五李四  50.0\n",
      "104  赵六王五  70.0\n",
      "105   NaN   NaN\n"
     ]
    }
   ],
   "source": [
    "df1 = pd.DataFrame(data={\"age\": [10, 20, 30, 40], \"name\": [\"张三\", \"李四\", \"王五\", \"赵六\"]},columns=[\"name\", \"age\"],index=[101, 102, 103, 104],)\n",
    "df2 = pd.DataFrame(data={\"age\": [10, 20, 30, 40], \"name\": [\"张三\", \"李四\", \"王五\", \"田七\"]},columns=[\"name\", \"age\"],index=[102, 103, 104, 105],)\n",
    "print(df1 + df2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   age name   id\n",
      "0   20   张三  101\n",
      "1   30   李四  102\n",
      "2   40   王五  103\n",
      "3   10   赵六  104\n",
      "     age name\n",
      "id           \n",
      "101   20   张三\n",
      "102   30   李四\n",
      "103   40   王五\n",
      "104   10   赵六\n"
     ]
    }
   ],
   "source": [
    "df = pd.DataFrame({\"age\": [20, 30, 40, 10], \"name\": [\"张三\", \"李四\", \"王五\", \"赵六\"], \"id\": [101, 102, 103, 104]})\n",
    "print(df)\n",
    "\n",
    "df.set_index(\"id\", inplace=True)#设置行索引\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    年龄  姓名\n",
      "id        \n",
      "一   20  张三\n",
      "二   30  李四\n",
      "三   40  王五\n",
      "四   10  赵六\n"
     ]
    }
   ],
   "source": [
    "df.rename(index={101: \"一\", 102: \"二\", 103: \"三\", 104: \"四\"}, columns={\"age\": \"年龄\", \"name\": \"姓名\"}, inplace=True)\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   年齡  名稱\n",
      "Ⅰ  20  张三\n",
      "Ⅱ  30  李四\n",
      "Ⅲ  40  王五\n",
      "Ⅳ  10  赵六\n"
     ]
    }
   ],
   "source": [
    "df.index = [\"Ⅰ\", \"Ⅱ\", \"Ⅲ\", \"Ⅳ\"]\n",
    "df.columns = [\"年齡\", \"名稱\"]\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   age name   id        phone\n",
      "0   20   张三  101  13333333333\n",
      "1   30   李四  102  14444444444\n",
      "2   40   王五  103  15555555555\n",
      "3   10   赵六  104  16666666666\n"
     ]
    }
   ],
   "source": [
    "df = pd.DataFrame({\"age\": [20, 30, 40, 10], \"name\": [\"张三\", \"李四\", \"王五\", \"赵六\"], \"id\": [101, 102, 103, 104]})\n",
    "df[\"phone\"] = [\"13333333333\", \"14444444444\", \"15555555555\", \"16666666666\"]\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   age name   id\n",
      "0   20   张三  101\n",
      "1   30   李四  102\n",
      "2   40   王五  103\n",
      "3   10   赵六  104\n"
     ]
    }
   ],
   "source": [
    "\n",
    "df.drop(\"phone\", axis=1, inplace=True)\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [],
   "source": [
    "os.makedirs(\"data\", exist_ok=True)\n",
    "df = pd.DataFrame({\"age\": [20, 30, 40, 10], \"name\": [\"张三\", \"李四\", \"王五\", \"赵六\"], \"id\": [101, 102, 103, 104]})\n",
    "df.set_index(\"id\", inplace=True)\n",
    "\n",
    "df.to_csv(\"data/df.csv\")\n",
    "df.to_csv(\"data/df.tsv\", sep=\"\\t\")# 设置分隔符为 \\t\n",
    "df.to_csv(\"data/df_noindex.csv\", index=False)# index=False 不保存行索引\n",
    "df.to_pickle(\"data/df.pkl\")\n",
    "df.to_excel(\"data/df.xlsx\")\n",
    "df.to_clipboard()\n",
    "df_dict = df.to_dict()\n",
    "df.to_hdf(\"data/df.h5\", key=\"df\")\n",
    "df.to_html(\"data/df.html\")\n",
    "df.to_json(\"data/df.json\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [],
   "source": [
    "# df.to_feather(\"data/df.feather\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "     age name\n",
      "id           \n",
      "101   20   张三\n",
      "102   30   李四\n",
      "103   40   王五\n",
      "104   10   赵六\n",
      "    id  age name\n",
      "0  101   20   张三\n",
      "1  102   30   李四\n",
      "2  103   40   王五\n",
      "3  104   10   赵六\n",
      "     age name\n",
      "id           \n",
      "101   20   张三\n",
      "102   30   李四\n",
      "103   40   王五\n",
      "104   10   赵六\n",
      "     age name\n",
      "id           \n",
      "101   20   张三\n",
      "102   30   李四\n",
      "103   40   王五\n",
      "104   10   赵六\n",
      "     age name\n",
      "id           \n",
      "101   20   张三\n",
      "102   30   李四\n",
      "103   40   王五\n",
      "104   10   赵六\n",
      "     age name\n",
      "101   20   张三\n",
      "102   30   李四\n",
      "103   40   王五\n",
      "104   10   赵六\n",
      "     age name\n",
      "id           \n",
      "101   20   张三\n",
      "102   30   李四\n",
      "103   40   王五\n",
      "104   10   赵六\n",
      "                   age               name\n",
      "id  Unnamed: 1_level_1 Unnamed: 2_level_1\n",
      "101                 20             å¼ ä¸\n",
      "102                 30             æå\n",
      "103                 40             çäº\n",
      "104                 10             èµµå",
      "­\n",
      "     age name\n",
      "101   20   张三\n",
      "102   30   李四\n",
      "103   40   王五\n",
      "104   10   赵六\n"
     ]
    }
   ],
   "source": [
    "#数据d导入\n",
    "df_csv = pd.read_csv(\"data/df.csv\", index_col=\"id\")# 指定行索引\n",
    "df_tsv = pd.read_csv(\"data/df.tsv\", sep=\"\\t\")# 指定分隔符\n",
    "df_pkl = pd.read_pickle(\"data/df.pkl\")\n",
    "df_excel = pd.read_excel(\"data/df.xlsx\", index_col=\"id\")\n",
    "df_clipboard = pd.read_clipboard(index_col=\"id\")\n",
    "df_from_dict = pd.DataFrame(df_dict)\n",
    "df_hdf = pd.read_hdf(\"data/df.h5\", key=\"df\")\n",
    "df_html = pd.read_html(\"data/df.html\", index_col=0)[0]\n",
    "df_json = pd.read_json(\"data/df.json\")\n",
    "# df_feather = pd.read_feather(\"data/df.feather\")\n",
    "\n",
    "print(df_csv)\n",
    "print(df_tsv)\n",
    "print(df_pkl)\n",
    "print(df_excel)\n",
    "print(df_clipboard)\n",
    "print(df_from_dict)\n",
    "print(df_hdf)\n",
    "print(df_html)\n",
    "print(df_json)\n",
    "# print(df_feather)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   gmv  trade_date        ymd\n",
      "0  100  2025-01-06 2025-01-06\n",
      "1  200  2023-10-31 2023-10-31\n",
      "2  300  2023-12-31 2023-12-31\n",
      "3  400  2023-01-05 2023-01-05\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "df = pd.DataFrame({\"gmv\":[100,200,300,400],\"trade_date\":[\"2025-01-06\",\"2023-10-31\",\"2023-12-31\",\"2023-01-05\"]})\n",
    "#2）将字符串字段转换为日期类型\n",
    "df[\"ymd\"] = pd.to_datetime(df[\"trade_date\"])\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   gmv  trade_date        ymd    yy  mm  dd\n",
      "0  100  2025-01-06 2025-01-06  2025   1   6\n",
      "1  200  2023-10-31 2023-10-31  2023  10  31\n",
      "2  300  2023-12-31 2023-12-31  2023  12  31\n",
      "3  400  2023-01-05 2023-01-05  2023   1   5\n"
     ]
    }
   ],
   "source": [
    "df['yy'],df['mm'],df['dd']=df['ymd'].dt.year,df['ymd'].dt.month,df['ymd'].dt.day\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   gmv  trade_date        ymd    yy  mm  dd      week\n",
      "0  100  2025-01-06 2025-01-06  2025   1   6    Monday\n",
      "1  200  2023-10-31 2023-10-31  2023  10  31   Tuesday\n",
      "2  300  2023-12-31 2023-12-31  2023  12  31    Sunday\n",
      "3  400  2023-01-05 2023-01-05  2023   1   5  Thursday\n"
     ]
    }
   ],
   "source": [
    "df['week']=df['ymd'].dt.day_name()\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   gmv  trade_date        ymd    yy  mm  dd      week  quarter\n",
      "0  100  2025-01-06 2025-01-06  2025   1   6    Monday        1\n",
      "1  200  2023-10-31 2023-10-31  2023  10  31   Tuesday        4\n",
      "2  300  2023-12-31 2023-12-31  2023  12  31    Sunday        4\n",
      "3  400  2023-01-05 2023-01-05  2023   1   5  Thursday        1\n",
      "   gmv  trade_date        ymd    yy  mm  dd      week  quarter   mend   yend\n",
      "0  100  2025-01-06 2025-01-06  2025   1   6    Monday        1  False  False\n",
      "1  200  2023-10-31 2023-10-31  2023  10  31   Tuesday        4   True  False\n",
      "2  300  2023-12-31 2023-12-31  2023  12  31    Sunday        4   True   True\n",
      "3  400  2023-01-05 2023-01-05  2023   1   5  Thursday        1  False  False\n"
     ]
    }
   ],
   "source": [
    "df['quarter']=df['ymd'].dt.quarter\n",
    "print(df)\n",
    "df['mend']=df['ymd'].dt.is_month_end\n",
    "df['yend']=df['ymd'].dt.is_year_end\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   gmv  trade_date        ymd    yy  mm  dd      week  quarter   mend   yend  \\\n",
      "0  100  2025-01-06 2025-01-06  2025   1   6    Monday        1  False  False   \n",
      "1  200  2023-10-31 2023-10-31  2023  10  31   Tuesday        4   True  False   \n",
      "2  300  2023-12-31 2023-12-31  2023  12  31    Sunday        4   True   True   \n",
      "3  400  2023-01-05 2023-01-05  2023   1   5  Thursday        1  False  False   \n",
      "\n",
      "  ystat    mstat   qstat                  wstat  \n",
      "0  2025  2025-01  2025Q1  2025-01-06/2025-01-12  \n",
      "1  2023  2023-10  2023Q4  2023-10-30/2023-11-05  \n",
      "2  2023  2023-12  2023Q4  2023-12-25/2023-12-31  \n",
      "3  2023  2023-01  2023Q1  2023-01-02/2023-01-08  \n"
     ]
    }
   ],
   "source": [
    "df[\"ystat\"] = df[\"ymd\"].dt.to_period(\"Y\")\n",
    "df[\"mstat\"] = df[\"ymd\"].dt.to_period(\"M\")\n",
    "df[\"qstat\"] = df[\"ymd\"].dt.to_period(\"Q\")\n",
    "df[\"wstat\"] = df[\"ymd\"].dt.to_period(\"W\")\n",
    "print(df)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
